package com.zzl.gulimall.product.service.impl;

import com.zzl.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.zzl.gulimall.product.entity.AttrEntity;
import com.zzl.gulimall.product.service.AttrAttrgroupRelationService;
import com.zzl.gulimall.product.service.AttrService;
import com.zzl.gulimall.product.vo.AttrRelationVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzl.common.utils.PageUtils;
import com.zzl.common.utils.Query;

import com.zzl.gulimall.product.dao.AttrGroupDao;
import com.zzl.gulimall.product.entity.AttrGroupEntity;
import com.zzl.gulimall.product.service.AttrGroupService;
import org.springframework.util.CollectionUtils;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    @Autowired
    private AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 条件分组查询 分组信息
     * @param params
     * @param catelogId
     * @return
     */
    @Override
    public PageUtils queryPageByCatelogId(Map<String, Object> params, Integer catelogId) {

        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(AttrGroupEntity::getCatelogId,catelogId);
        String key = (String) params.get("key");
        if (StringUtils.isNotBlank(key)){
            wrapper.and( w ->{
                w.lambda().eq(AttrGroupEntity::getAttrGroupId,key).or().like(AttrGroupEntity::getAttrGroupName,key);
            });
        }
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                wrapper
        );
        return new PageUtils(page);
    }

    @Override
    public PageUtils getAttrRelationList(Map<String, Object> params, Long attrgroupId, String type) {

        List<AttrAttrgroupRelationEntity> relationEntityList = attrAttrgroupRelationService.list(new QueryWrapper<AttrAttrgroupRelationEntity>().lambda().eq(AttrAttrgroupRelationEntity::getAttrGroupId, attrgroupId));
        List<Long> attrIds = relationEntityList.stream().map(AttrAttrgroupRelationEntity::getAttrId).collect(Collectors.toList());

        QueryWrapper<AttrEntity> wrapper = new QueryWrapper<>();
        if ("attr".equalsIgnoreCase(type)){
            wrapper.lambda().in(AttrEntity::getAttrId,attrIds);
        }else {
            wrapper.lambda().notIn(AttrEntity::getAttrId,attrIds);
        }
        IPage<AttrEntity> page = attrService.page(
                new Query<AttrEntity>().getPage(params),
               wrapper);

        return new PageUtils(page);
    }

    @Override
    public void saveAttrRelation(List<AttrRelationVo> attrRelationVoList) {
        if (!CollectionUtils.isEmpty(attrRelationVoList)){
            List<AttrAttrgroupRelationEntity> relationEntityList = attrRelationVoList.stream().map(vo -> {
                AttrAttrgroupRelationEntity relation = new AttrAttrgroupRelationEntity();
                BeanUtils.copyProperties(vo,relation);
                return relation;
            }).collect(Collectors.toList());
            attrAttrgroupRelationService.saveBatch(relationEntityList);
        }
    }

    @Override
    public void removeAttrRelation(List<AttrRelationVo> attrRelationVoList) {
        List<AttrAttrgroupRelationEntity> relationEntityList = attrRelationVoList.stream().map(vo -> {
            AttrAttrgroupRelationEntity relation = new AttrAttrgroupRelationEntity();
            BeanUtils.copyProperties(vo,relation);
            return relation;
        }).collect(Collectors.toList());
        attrAttrgroupRelationService.removeAttrRelation(relationEntityList);
    }


}